{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "\n",
    "import gym\n",
    "import random\n",
    "import torch\n",
    "import numpy as np\n",
    "from collections import deque\n",
    "import matplotlib.pyplot as plt\n",
    "from src.PPO.PPO import PPO, PPOContinuous\n",
    "import pdb\n",
    "import time\n",
    "\n",
    "\n",
    "\n",
    "# env_name = \"BipedalWalker-v2\"\n",
    "env_name = \"Pendulum-v0\"\n",
    "env = gym.make(env_name)\n",
    "\n",
    "env.seed(10)\n",
    "\n",
    "state_size = env.observation_space.shape[0]\n",
    "action_size =env.action_space.shape[0]\n",
    "\n",
    "print(state_size)\n",
    "print(action_size)\n",
    "\n",
    "# PPO Settings\n",
    "update_every = 4000\n",
    "num_learn = 80\n",
    "win_condition = 300\n",
    "\n",
    "# Agent settings\n",
    "hidden_size=128\n",
    "epsilon=0.2\n",
    "entropy_beta=0.01\n",
    "gamma=0.99\n",
    "lr=0.0003\n",
    "\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = PPOContinuous(state_size, action_size, hidden_size=hidden_size, epsilon=epsilon, entropy_beta=entropy_beta, gamma=gamma, lr=lr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "IncompatibleKeys(missing_keys=[], unexpected_keys=[])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# model_path=\"trained_models/ppo/walker_ppo_model.pth\"\n",
    "# model_old_path=\"trained_models/ppo/walker_ppo_model_old.pth\"\n",
    "\n",
    "model_path=\"trained_models/ppo/Pendulum-v0_ppo_model.pth\"\n",
    "model_old_path=\"trained_models/ppo/Pendulum-v0_ppo_model_old.pth\"\n",
    "\n",
    "agent.model.load_state_dict(torch.load(model_path))\n",
    "agent.model_old.load_state_dict(torch.load(model_old_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "tensor(-125.6985)\n"
     ]
    }
   ],
   "source": [
    "env = gym.wrappers.Monitor(env, \"./vid\", video_callable=lambda episode_id: True,force=True)\n",
    "\n",
    "for episode in range(1):\n",
    "    state = env.reset()\n",
    "    score = 0\n",
    "    input()\n",
    "\n",
    "    for t in range(700):\n",
    "        actions_tensor, log_prob = agent.act(torch.FloatTensor(state))\n",
    "        actions = actions_tensor.cpu().data.numpy().flatten()\n",
    "        next_state, reward, done, _ = env.step(actions_tensor)\n",
    "\n",
    "        \n",
    "        score += reward\n",
    "        \n",
    "        env.render()\n",
    "        \n",
    "        if done:\n",
    "            break;\n",
    "        \n",
    "        state = next_state\n",
    "\n",
    "    print(score)\n",
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
